package com.algorithm;

/**
 * 买卖股票的最佳时机 II
 */
public class bestTimeToBuyAndSellStockIi {

    public static int maxProfit(int[] prices) {
        if (prices.length==0){
            return 0;
        }
        int lowPrice=prices[0];
        int hightPrice=prices[0];
        int totalPrice=0;
        for (int i = 1; i < prices.length; i++) {
            if (prices[i]<prices[i-1]){
                totalPrice=totalPrice+hightPrice-lowPrice;
                lowPrice=prices[i];
                hightPrice=prices[i];
            }else {
                hightPrice=prices[i];
            }
        }
        return totalPrice+hightPrice-lowPrice;
    }

    public static void main(String[] args) {
//        int [] nums=new int[]{7,1,5,3,6,4};
        int [] nums=new int[]{1,2,3,4,5};
        int reuslt = maxProfit(nums);
        System.out.println(reuslt);
    }
}
